package com.nbstar.film.spider;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import com.nbstar.film.spider.domain.FilmSpiderDomain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 湖北cdn数据删除，删除近一月未访问的数据
 */
@Slf4j
public class CdnDataFinder {

    private List<String> keepContendIdList = new ArrayList<>();

    private boolean checkContentIdValid(String contentId) {
        for (String del : keepContendIdList) {
            if (del.equals(contentId)) {
                return true;
            }

        }

        return false;
    }


    public void findOffContentId(String csvFilePath, String resultCsvFile) {
        try {
            // 用来保存数据
            ArrayList<String[]> csvFileList = new ArrayList<String[]>();
            CsvWriter csvWriter = new CsvWriter(resultCsvFile, ',', Charset.forName("UTF-8"));
            String[] csvHeaders = { "ContentId"};
            csvWriter.writeRecord(csvHeaders);

            // 创建CSV读对象 例如:CsvReader(文件路径，分隔符，编码格式);
            CsvReader reader = new CsvReader(csvFilePath, ',', Charset.forName("UTF-8"));
            // 跳过表头 如果需要表头的话，这句可以忽略
            reader.readHeaders();
            // 逐行读入除表头的数据
            while (reader.readRecord()) {
                String[] lines = reader.getValues();
                String contentId = null;
                if (lines.length >= 3) {
                    String[] items = lines[2].split("&");

                    for (String c : items) {
                        if (c.contains("Contentid=")) {
                            contentId = c.replace("Contentid=", "");
                            System.out.println("ContentId1=--->" + contentId);
                            break;
                        }
                    }

                    if (contentId == null) {
                        contentId = lines[0].split("/")[1];
                        contentId = contentId.replace("_mdn_2", "");
                        contentId = contentId.replace("_mdn_1", "");
                        System.out.println("ContentId2=--->" + contentId);
                    }
                } else {
                    String items[] = lines[0].split("/");
                    contentId = items[1];
                    contentId = contentId.replace("_mdn_2", "");
                    contentId = contentId.replace("_mdn_1", "");
                    System.out.println("content id:" + contentId);
                }

                if (!checkContentIdValid(contentId)) {
                    String[] csvContent = { contentId };
                    csvWriter.writeRecord(csvContent);
                }
                //System.out.println("………………………………" + reader.getRawRecord());
                //csvFileList.add(reader.getValues());
            }
            reader.close();
            csvWriter.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void readExcel2Keeplist(String excelPath, int sheetIndex, int startRow, int colIndex) throws IOException {
        XSSFWorkbook wb = null;
        XSSFSheet sheet = null;
        LinkedBlockingQueue<FilmSpiderDomain> results = new LinkedBlockingQueue<>();

        wb = new XSSFWorkbook(new FileInputStream(excelPath));
        sheet = wb.getSheetAt(sheetIndex);
        int rowNum = sheet.getLastRowNum();
        log.info("获取到行号：{}", rowNum);
        for (int i = startRow; i < rowNum+1; i++) {

            XSSFRow row = sheet.getRow(i);
            // 获取行里面的总列数
            int columnNum = 0;
            if (row != null) {
                columnNum = row.getPhysicalNumberOfCells();
            }

            if (columnNum > 0) {
                try {
                    //String cellValue = cell.getRawValue();//.getStringCellValue();
                    //RawFromExcel rawFromExcel = readRowFromExcel(row);
                    String url = row.getCell(colIndex).getStringCellValue();

                    if (!StringUtils.isEmpty(url) && url.indexOf("Contentid=") >= 0) {

                        String contentId = url.substring(url.indexOf("Contentid=") + "Contentid=".length());
                        keepContendIdList.add(contentId);
                    } else if (!StringUtils.isEmpty(url)) {
                       // deleteContentIdList.add(url);
                        System.out.println("Found contentId:" + url);
                    }
                } catch (Exception e) {
                    log.error("Found error at {}, {}" ,i, e.getMessage());
                }
            }
        }

    }

    @Test
    public void start() throws IOException {
        readExcel2Keeplist("E:\\湖北咪视通cdn内容删除\\咪视通3近一月半访问的内容内容id.xlsx", 0, 1, 0);
        readExcel2Keeplist("E:\\湖北咪视通cdn内容删除\\咪视通4近一月半访问的魔百和id.xlsx", 0, 1, 0);

        log.info("发现屏蔽和版权到期的内容id:" + keepContendIdList.size() + "条");

        //findOffContentId("E:\\湖北咪视通cdn内容删除\\咪视通400.csv", "E:\\湖北咪视通cdn内容删除\\咪视通400-20210126-result.csv");
        findOffContentId("E:\\湖北咪视通cdn内容删除\\咪视通210.csv", "E:\\湖北咪视通cdn内容删除\\咪视通210-20210126-result.csv");

    }

}
